//
//  LatestCollectionViewCell.m
//  项目一
//
//  Created by 王鑫 on 15/11/12.
//  Copyright (c) 2015年 王鑫. All rights reserved.
//

#import "LatestCollectionViewCell.h"
#import "LatestTableViewCell.h"
#import "LatestDetailViewController.h"
#import "AFNetworking.h"
#import "LatestModel.h"
#import "UIImageView+WebCache.h"
#import "ScrollerViewModel.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

@implementation LatestCollectionViewCell
#pragma mark - Method dealloc
- (void)dealloc {
    [_tableView release];
    [_scrollerArr release];
    [_scrollViewPic release];
    [_page release];
    [_pageNumber release];
    [_latestArr release];
    [_tempArr release];
    [_arr release];
    [_header release];
    [_footer release];
    [_timer release];
    [super dealloc];
}
#pragma mark - Method init With Frame
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 设置tableView
        [self createTableView];
    }
    return self;
}
#pragma mark - Method create TableView
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    [self.contentView addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [_tableView release];
    // 对数组进行初始化
    self.arr = [NSMutableArray array];
    self.scrollerArr = [NSMutableArray array];
    // 下拉刷新, 上拉加载
    [self RefreshingUpOrDown];
}
#pragma mark - Method RefreshingUpOrDown
- (void)RefreshingUpOrDown {
    // 下拉刷新
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 第一个页面的连接数字是0
        self.pageNumber = @"0";
        // 刷新数据
        [self createData];
    }];
    // 上拉加载
    self.tableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        // 找到最后一个key - value的字符串
        self.pageNumber = [[self.arr lastObject] lasttime];//  没有那个model
        // 刷新数据
        [self createData];
    }];
    // 下拉刷新, 开始进行刷新
    [self.tableView.header beginRefreshing];
}
#pragma mark - Method height For Row At IndexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}
#pragma mark - Method number Of Rows In Section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arr.count;
}
#pragma mark - Method cell For Row At IndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    LatestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[LatestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    }
    LatestModel *temp = self.arr[indexPath.row];
    cell.model = temp;
    return cell;
}
#pragma mark - Method layoutSubviews
- (void)layoutSubviews {
    [super layoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
}
#pragma mark - Method createData
- (void)createData {
    NSString *strUrl = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov5.0.0/news/newslist-pm1-c0-nt0-p1-s30-l%@.json", self.pageNumber];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSMutableArray *newlistArray = dic[@"result"][@"newslist"];
        NSMutableArray *scrollShowArr = dic[@"result"][@"focusimg"];
        self.tempArr = [NSMutableArray array];
        for (NSDictionary *tempDic in newlistArray) {
            LatestModel  *model = [[LatestModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.tempArr addObject:model];
            [model release];
        }
        for (NSDictionary *scrollerViewDic in scrollShowArr) {
            ScrollerViewModel *scrollerModel = [[ScrollerViewModel alloc] init];
            [scrollerModel setValuesForKeysWithDictionary:scrollerViewDic];
            [self.scrollerArr addObject:scrollerModel];
            [scrollerModel release];
        }
        // 判断是不是第一个链接
        if ([self.pageNumber isEqualToString:@"0"]) {
            // 是第一个链接, 则数组保持原来的个数
            self.arr = self.tempArr;
            [self.tableView.header endRefreshing];
        } else {
            // 不是第一个链接, 添加数组
            [self.arr addObjectsFromArray:self.tempArr];
            [self.tableView.footer endRefreshing];
        }
        [self.tableView reloadData];
        [self createScrollerView];
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];

    }];
}
//#warning PAGE 的设置方式
#pragma mark - Method createScrollerView
- (void)createScrollerView {
    self.scrollViewPic = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    self.scrollViewPic.backgroundColor = [UIColor yellowColor];
    self.tableView.tableHeaderView = self.scrollViewPic;
    self.scrollViewPic.contentSize = CGSizeMake(self.scrollerArr.count * WIDTH, 0);
    self.scrollViewPic.pagingEnabled = YES;
    self.scrollViewPic.delegate = self;
    
    for (NSInteger i = 0; i < self.scrollerArr.count; i++) {
        ScrollerViewModel *scrollerView = self.scrollerArr[i];
        UIImageView *photoScrollerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, 200)];
        photoScrollerImageView.userInteractionEnabled = YES;
        [self.scrollViewPic addSubview:photoScrollerImageView];
        [photoScrollerImageView sd_setImageWithURL:[NSURL URLWithString:scrollerView.imgurl]];
        [photoScrollerImageView release];
    }
    UIImageView *lastImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * self.scrollerArr.count, 0, WIDTH, 200)];
    [lastImageView sd_setImageWithURL:[NSURL URLWithString:[self.scrollerArr[0] imgurl]]];
    [self.scrollViewPic addSubview:lastImageView];
}
#pragma mark - Method changeImage
- (void)changeImage {
    [self.scrollViewPic setContentOffset:CGPointMake(self.scrollViewPic.contentOffset.x + WIDTH, 0) animated:YES];
    if (self.scrollViewPic.contentOffset.x == WIDTH * self.scrollerArr.count) {
        self.scrollViewPic.contentOffset = CGPointMake(0, 0);
    }
}
#pragma mark - Method scrollViewDidEndDecelerating
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (self.scrollViewPic.contentOffset.x == WIDTH * self.scrollerArr.count) {
        self.scrollViewPic.contentOffset = CGPointMake(WIDTH, 0);
    } else if (self.scrollViewPic.contentOffset.x == 0) {
        self.scrollViewPic.contentOffset = CGPointMake(self.scrollerArr.count *WIDTH, 0);
    }
    self.page.currentPage = self.scrollViewPic.contentOffset.x / WIDTH - 1;
}
#pragma mark - Method did Select Row At IndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.latestDelegate LatestPushToWeb:self.tempArr[indexPath.row]];
}
@end
